/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bea.cliente.environment;
import bea.cliente.environment.arena.Arena;
import bea.cliente.core.Game;
import bea.cliente.core.Memory;
import bea.cliente.character.Character;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;
import com.jme3.util.SkyFactory;
import com.jme3.water.WaterFilter;
import java.util.HashMap;

/**
 *
 * @author Ivan
 */
public class WorldManager {

    private Game game;
    // personagem atual na arena
    private Character character;
    // array com os nomes das arenas disponiveis
    private String[] arenasFileName = {"skyArena"};
    // hash map que guarda as arenas associadas aos seus nomes
    private HashMap<String, Arena> arenas;
    // array com os nomes dos skyboxes referente a cada arena
    private String[] skyTextures = {"Textures/Sky/Dome/cloudWispy.png"};
    // hash map que contera todos os céus de acordo com a ordem das arenas
    private HashMap<String, Spatial> skys;
    // agua do mundo
    private WaterFilter sea;
    private float seaHeight = -5.0f;
    private float initialSeaHeight = 0.8f;
    
    private String currentArena;

    public WorldManager() {
        this.game = Memory.get("game");
    }

    public void initWorld() {
        // inicializa o hashmap para armazenar as arenas
        arenas = new HashMap<String, Arena>();
        // inicializa as luzes do game
        initLights();
        // inicializa os "céus"
        initSkys();
        // carrega as arenas
        loadArenas();
    }
    
    private void initLights(){
        DirectionalLight dl = new DirectionalLight();
        Vector3f lightDir = new Vector3f(-1, -1, .5f).normalizeLocal();
        dl.setDirection(lightDir);
        dl.setColor(new ColorRGBA(.9f, .9f, .9f, 1));
        game.getRootNode().addLight(dl);

        dl = new DirectionalLight();
        dl.setDirection(new Vector3f(1, 0, -1).normalizeLocal());
        dl.setColor(new ColorRGBA(1, 1, 1, 1));
        dl.setColor(new ColorRGBA(.4f, .4f, .4f, 1));
        game.getRootNode().addLight(dl);
    }
    
    private void initSkys(){
        skys = new HashMap<String, Spatial>();
        
        for(int i = 0; i < skyTextures.length; i++){
            Spatial sky = SkyFactory.createSky(game.getAssetManager(), skyTextures[i], true);
            skys.put(arenasFileName[i], sky);
            game.getRootNode().attachChild(sky);
        }
    }
    
    private void loadArenas(){
        for(String arenaName :arenasFileName){
            Arena arena = new Arena(arenaName);
            arena.init(game);
            // adiciona no nodo raiz
            game.getRootNode().attachChild(arena);
            // adiciona o controle do ambiente fisico
            // da arena ao ambiente fisico do game
            game.getPhysicsSpace().add(arena.getPhysicsControl());
            // adiciona a arena ao hashmap
            arenas.put(arena.getName(), arena);
        }
        currentArena = arenasFileName[0];
    }
    
    public Arena getCurrentArena(){
        return arenas.get(currentArena);
    }
}
